package own.stu.jobgib.playown.alg.leetcode.editor.cn;

import java.util.*;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {

    }

    public static int findRadius(int[] houses, int[] heaters) {
        if (houses == null || houses.length == 0 || heaters == null || heaters.length == 0) {
            return 0;
        }
        TreeSet<Integer> set = new TreeSet<>();
        for (int i : heaters) {
            set.add(i);
        }
        int ans = 0;
        for (int h : houses) {

            Integer ceil = set.ceiling(h);
            Integer floor = set.floor(h);
            int curAns = Integer.MAX_VALUE;
            if(ceil != null){
                curAns = ceil - h;
            }
            if(floor != null){
                curAns = Math.min(curAns, h - floor);
            }
            ans = Math.max(ans, curAns);
        }
        return ans;
    }

    public static int captureForts(int[] forts) {
        if(forts == null || forts.length == 0){
            return 0;
        }

        // 找相邻1和-1的最大距离
        int n = forts.length;
        int ans = 0;
        int i = 0;
        for(; i < n; i ++){
            if(forts[i] == 0) continue;
            int j = i + 1;
            for(; j < n; j ++){
                if(forts[j] == 0) continue;
                if(forts[i] != forts[j]){
                    ans = Math.max(ans, j - i - 1);
                }
                i = j - 1;
                break;
            }
        }
        return ans;
    }

    static class Pair{
        int v1;
        int v2;
        public Pair(int v1, int v2){
            this.v1 = v1;
            this.v2 = v2;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Pair pair = (Pair) o;
            return v1 == pair.v1 &&
                    v2 == pair.v2;
        }

        @Override
        public int hashCode() {
            return Objects.hash(v1, v2);
        }
    }

    public static void main(String[] args) {

//        HashSet<Pair> set = new HashSet<>();
//        System.out.println(set.add(new Pair(1,2)));
//        System.out.println(set.add(new Pair(1,2)));

        /*HashSet<Integer> set = new HashSet<>();
        System.out.println(set.add(129));
        System.out.println(set.add(129));

        Integer l1 = new Integer(340);
        Integer l2 = new Integer(340);
        System.out.println(l1 == l2);
        System.out.println(l1.equals(l2));*/

        HashMap<Integer, List<Integer>> map = new HashMap<>();
        map.computeIfAbsent(1, (v) -> {
            ArrayList<Integer> list = new ArrayList<>();
            list.add(0);
            return list;
        });

        map.compute(1, (k, v) -> {

            ArrayList<Integer> list = new ArrayList<>();
            list.add(2);
            return list;
        });

        map.getOrDefault(2, new ArrayList<>()).add(100);
        map.getOrDefault(2, new ArrayList<>()).add(33);

        System.out.println(map);

//        System.out.println(captureForts(new int[]{1,0,0,-1,0,0,0,0,1}));
//        System.out.println(findRadius(new int[]{1, 2, 3, 4}, new int[]{1, 4}));
//        System.out.println(findRadius(new int[]{1, 5}, new int[]{2}));
        /*TreeSet<Integer> set = new TreeSet<>();
        set.add(1);
        set.add(2);
        set.add(4);
        set.add(5);
        set.add(7);
        set.add(14);

        System.out.println(set.floor(4));
        System.out.println(set.ceiling(4));
        System.out.println(set.floor(9));
        System.out.println(set.ceiling(9));
        System.out.println(set.floor(12));
        System.out.println(set.ceiling(12));
        System.out.println(set.ceiling(-1));
        System.out.println(set.ceiling(null));*/
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}